<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户id">
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="商户名称">
          <el-input v-model="form.merchant_name" placeholder="商户名称" />
        </el-form-item>
        <el-form-item label="发卡渠道">
          <el-select
            clearable
            v-model="form.issuer_channel"
            placeholder="发卡渠道"
          >
            <el-option
              v-for="item in issueChannelData"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column prop="merchant_id" label="商户id" align="center" />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column prop="name" label="名称" align="center" />
          <el-table-column prop="en_name" label="英文名称" align="center" />
          <el-table-column
            prop="region"
            label="区域(内部备注)"
            align="center"
          />
          <el-table-column
            prop="business"
            label="业务场景(内部备注)"
            align="center"
          />
          <el-table-column
            prop="card_type"
            label="卡种类(内部备注)"
            align="center"
          />
          <el-table-column
            prop="brand"
            label="卡品牌(内部备注)"
            align="center"
          />
          <el-table-column
            prop="issuer_channel"
            label="发卡渠道"
            align="center"
          />
          <el-table-column
            prop="issuer_client"
            label="渠道API对接商户"
            align="center"
          />
          <el-table-column
            prop="card_rate"
            label="开卡手续费率"
            align="center"
          />
          <el-table-column
            prop="card_usd_fee"
            label="开卡美金费用"
            align="center"
          />
          <el-table-column
            prop="is_offline"
            label="是否线下开卡"
            align="center"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_offline ? 'success' : 'danger'">{{
                scope.row.is_offline ? "是" : "否"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right">
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :issueChannelData="issueChannelData"
      :issueClientData="issueClientData"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import {
  cardsMerchantConfigApi,
  cardsMerchantConfigUpdateApi,
} from "@/api/ExAssistant/cards";
import DialogForm from "./components/configForm.vue";

const loading = ref(false);
const tableData = ref([]);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);
const issueChannelData = ref([]);
const issueClientData = ref([]);
const formData = {
  merchant_id: "",
  issuer_channel: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  id: "",
  merchant_id: "",
  name: "",
  en_name: "",
  region: "",
  business: "",
  card_type: "",
  brand: "",
  issuer_channel: "",
  issuer_client: "",
  card_rate: 0,
  card_usd_fee: 0,
  is_offline: "",
};
const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    issuer_channel: form.issuer_channel,
  };
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await cardsMerchantConfigApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    issueChannelData.value = res.data.extra.issuer_channel;
    issueClientData.value = res.data.extra.issuer_client;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 新增
const onAdd = async () => {
  dialogVisible.value = true;
  btnType.value = "add";
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  delete param.src;
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;
      const res = await cardsMerchantConfigUpdateApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      //console.log(param);
      const res = await cardsMerchantConfigUpdateApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
  }
};

const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    Object.assign(dialogForm, { ...row });
    dialogVisible.value = true;
  }
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
